﻿using DoNet.Bpmn.Core.Enums;
using DoNet.Bpmn.Core.Utils;
using DoNet.Bpmn.Core.Vo;

namespace DoNet.Bpmn.Adaptor.BpmnElement
{
    public class BpmnElementPersonnelAdaptor : BpmnElementAdaptor
    {
        protected override BpmnConfCommonElementVo? GetElementVo(BpmnNodePropertysVo? property, BpmnNodeParamsVo? nodeParamsVo, int elementCode, string elementId)
        {
            int? signType = null;
            if (property != null)
            {
                signType = property.SignType;
            }

            string collectionName = "personnelList";


            var assigneeMap = new SortedDictionary<string, string>();
            var assignee = new List<string>();

            if (nodeParamsVo != null)
            {
                List<BpmnNodeParamsAssigneeVo> assigneeList = nodeParamsVo.AssigneeList;
                if (assigneeList.Any())
                {
                    string elementName = assigneeList[0].ElementName;

                    foreach (var assigneeVo in assigneeList)
                    {
                        if (assigneeVo.IsDeduplication == 0)
                        {
                            assignee.Add(assigneeVo.Assignee);
                            assigneeMap[assigneeVo.Assignee] = assigneeVo.AssigneeName;
                        }
                    }

                    string elementCodeStr = string.Join("", collectionName, elementCode);
                    if (signType == (int)SignTypeEnum.SIGN_TYPE_SIGN)
                    {
                        return BpmnElementUtils.GetMultiplayerSignElement(elementId, elementName, elementCodeStr, assigneeMap.Keys.ToList(), assigneeMap);
                    }
                    else if (signType == (int)SignTypeEnum.SIGN_TYPE_SIGN_IN_ORDER)
                    {
                        return BpmnElementUtils.GetMultiplayerSignInOrderElement(elementId, elementName, elementCodeStr, assigneeMap.Keys.ToList(), assigneeMap);
                    }
                    else
                    {
                        return BpmnElementUtils.GetMultiplayerOrSignElement(elementId, elementName, elementCodeStr, assigneeMap.Keys.ToList(), assigneeMap);
                    }
                }
                else
                {
                    Console.Error.WriteLine("Assignee list is empty or null.");
                    //return null; // Or handle the error as needed
                }
            }
            return default;
        }

        public override void SetSupportBusinessObjects()
        {
            ((IAdaptorService)this).AddSupportBusinessObjects(NodePropertyEnum.NODE_PROPERTY_PERSONNEL);
        }
    }
}
